﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading;
using System.Windows.Input;
using VMDS.GUIClient.Helper;
using VMDS.GUIClient.MainServiceReference;
using VMDS.GUIClient.Model;
using VMDS.GUIClient.ServiceReference;

namespace VMDS.GUIClient.ViewModels
{
    class DialogViewModel:BaseViewModel
    {

        private bool _visible;
        public bool Visible
        {
            get { return _visible; }
            set { _visible = value;OnPropertyChanged(()=>Visible); }
        }

        private bool _progBar;
        public bool ProgressBar
        {
            get { return _progBar; } 
            set { _progBar = value; OnPropertyChanged(()=>ProgressBar); }
        }

        private int _progress;
        public int Progress
        {
            get { return _progress; }
            set { _progress = value;OnPropertyChanged(()=>Progress); }
        }

        private bool _isIndeterm;
        public bool IsIndeterminate
        {
            get { return _isIndeterm; }
            set { _isIndeterm = value;OnPropertyChanged(()=>IsIndeterminate); }
        }

        private string _msg;
        public string Message
        {
            get { return _msg; }
            set { _msg = value;OnPropertyChanged(()=>Message); }
        }
        #region OkButton
        private bool _ok;
        public bool OkButton
        {
            get { return _ok; }
            set { _ok = value; OnPropertyChanged(() => OkButton); }
        }
       
        public ICommand OkClick { get { return new BaseCommand(OkClickAction,OkActionCanExecute); } }
        protected virtual void OkClickAction(){}
        protected virtual bool OkActionCanExecute() { return true; }
        #endregion

        #region Cancel Button
        private bool _cancel;
        public bool CancelButton
        {
            get { return _cancel; }
            set { _cancel = value; OnPropertyChanged(() => CancelButton); }
        }
        public ICommand CancelClick { get { return new BaseCommand(CancelClickAction,CanCancelAction); } }
        protected virtual void CancelClickAction(){}
        protected virtual bool CanCancelAction(){return true;}
        #endregion

        #region Yes Button
       
        private bool _yes;
        public bool YesButton
        {
            get { return _yes; }
            set { _yes = value; OnPropertyChanged(() => YesButton); }
        }
        public ICommand YesClick { get { return new BaseCommand(YesClickAction, CanYesAction); } }
        protected virtual void YesClickAction() { }
        protected virtual bool CanYesAction() { return true; }
        #endregion

        #region No Button
       
        private bool _no;
        public bool NoButton
        {
            get { return _no; }
            set { _no = value; OnPropertyChanged(() => NoButton); }
        }
        public ICommand NoClick { get { return new BaseCommand(NoClickAction, CanNoAction); } }
        protected virtual void NoClickAction() { }
        protected virtual bool CanNoAction() { return true; }
        #endregion

        public DialogViewModel()
        {
            OkButton = true;
            CancelButton = true;
            Visible = true;
        }


    }

    internal class ConnectionDialog:DialogViewModel
    {
        private BackgroundWorker _worker;
       
       
        public ConnectionDialog()
        {
           /* if (MsLocator.Completed)
                try
                {
                    MsLocator.Connect();
                    GetData();
                    Visible = false;
                }
                catch (Exception)
                {
                    Message = "В данный момент служба недоступна";
                    CancelButton = false;
                    OkButton = true;

                }*/
            Message = "Подождите, пожалуйста, идет подключение... ";
            ProgressBar = true;
            Progress = 0;
            IsIndeterminate = true;
            OkButton = false;
            CancelButton = true;
            _worker=new BackgroundWorker(){WorkerSupportsCancellation = true};
            _worker.DoWork += new DoWorkEventHandler(_worker_DoWork);
            _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);
            _worker.RunWorkerAsync();

        }


        void GetData()
        {
            //ManagerDataStore.Instance.Storages = new ObservableCollection<Storage>(MsLocator.Client.GetStorages());
        
            ManagerDataStore.Instance.LoadData();
        }

        private bool _connectResult;
        void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (_connectResult)
            {
                
                Progress = 40;
                IsIndeterminate = false;
                try
                {
                    ServiceLocator.MainService.TestRights();
                }
                catch
                {
                    Message = "Недостаточно прав.\nДанный раздел будет недоступен";
                    ProgressBar = false;
                    OkButton = true;
                    CancelButton = false; return;
                    
                }
                Message = "Получение данных...";
                GetData();
                Visible = false;
            }
            else
            {
                Message = "Не удалось подключиться.\nДанный раздел будет недоступен";
                ProgressBar = false;
                OkButton = true;
                CancelButton = false;
            }
        }

        void _worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //var ms = new MainServiceContractClient(new NetTcpBinding(),new EndpointAddress("net.tcp://localhost:1234/vmds"));
            var ms = ServiceLocator.MainService;
            try
            {
                ms.GetShareUNC();
                _connectResult = true;
            }
            catch (Exception)
            {
                _connectResult = false;
            }
        }
        protected override void OkClickAction()
        {
            if (CrossWindowConnector.MainView != null)
                ((MainWindowViewModel) CrossWindowConnector.MainView).VmSelect.Execute(null);
        }
        

        protected override void CancelClickAction()
        {
            OkClickAction();
        }
    }

}
